package com.hua.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.hua.entity.Entry;
import com.hua.service.EntryService;

import com.hua.utils.Result;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smion
 * @since 2022-07-07
 */
@Controller
@ResponseBody
@RestController
@RequestMapping("/api/entry")
public class EntryController {

    @Resource
    private EntryService entryService;


    @PostMapping("/add")
    public Result add(@RequestBody Entry entry){
        entry.setCreatetime(new Date());
        if (entryService.save(entry)){
            return Result.ok().message("添加成功");
        }
        return Result.Error().message("添加失败");
    }

    @GetMapping("/listAll")
    public Result listAll(){
        List list = entryService.list();
        return Result.ok(list).message("查询成功");
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        System.out.println(id);
        if (entryService.removeById(id)){
            return Result.ok().message("删除成功");
        }
        return Result.Error().message("删除失败");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Entry entry){
        entry.setUpdatetime(new Date());
        if (entryService.updateById(entry)){
            return Result.ok().message("更新成功");
        }
        return Result.Error().message("更新失败");
    }

    /**
     * 根据id查询数据
     */
    @PostMapping("/selectId")
    public Result selectId(@PathVariable String id){
        Entry entry = entryService.getById(id);
        if (entry!=null){
            System.out.println(entry);
            return Result.ok(entry).message("查询成功");
        }
        return Result.Error().message("查询失败");
    }


    /**
     * 测试
     * @return
     */
    @GetMapping("/app")
    public Result app() {
        List list = entryService.getadd();
        if (list != null) {
            return Result.ok(list).message("成功");
        }
        return Result.Error().message("失败");
    }

    /**
     * 导出数据
     */
    @GetMapping(value = "/export",produces = "application/octet-stream")
    public  void exportEmployee(HttpServletResponse response){
        List list = entryService.list();
        System.out.println(response);
        ExportParams params = new ExportParams("员工表","员工表", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, Entry.class, list);
        ServletOutputStream out = null;
        try {
            //流形式
            response.setHeader("content-type","application/octet-stream");
            //防止中文乱码
            response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(
                    "员工表.xls","UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (out!=null){
                try {
                    out.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

}

